<template>
  <view>
    <kt-nav-bar
        :title="$i18n.enToGlobal('test page')"
    ></kt-nav-bar>
    <view style="height: 50rpx"></view>
    <view class="box">
      <kt-button
          border-radius="20rpx"
          @click="googlePay()">Google Pay
      </kt-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: true
    }
  },
  mounted() {
  },
  methods: {
    googlePay() {

      plus.payment.getChannels((providers) => {
        let provider = providers.find(function (e) {
          return e.id === "google-pay";
        });

        let paymentMethodType = "CARD";

        let cardPaymentMethodConfig = {
          environment: 3, // 必填 1 是product  3是test
          paymentMethodType: paymentMethodType, //必填 CARD、PAYPAL
          existingPaymentMethodRequired: false, //可选 如果设置为true同时已经准备好了支付allowedPaymentMethods中的付款方式，isReadyToPay就会返回true。

          currencyCode: "USD", //必填
          countryCode: "CN", //在欧洲经济区必填
          transactionId: "", //当你想要接收googlepay回调的时候必填
          totalPriceStatus: "FINAL", //必填  NOT_CURRENTLY_KNOWN、ESTIMATED、FINAL
          totalPrice: "1.00", //必填 满足正则格式^[0-9]+(\.[0-9][0-9])?$
          totalPriceLabel: "100heelo", //可选
          checkoutOption: "DEFAULT", //可选 DEFAULT、COMPLETE_IMMEDIATE_PURCHASE

          merchantName: "Example Merchant", //可选
          emailRequired: true, //可选
          shippingAddressRequired: true, //可选
          shippingPhoneNumberRequired: false, //可选
          allowedCountryCodes: [], //可选
          allowedAuthMethods: ["PAN_ONLY", "CRYPTOGRAM_3DS"], //必填
          allowedCardNetworks: ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"], //必填
          allowPrepaidCards: false, //可选
          // allowCreditCards:false,//可选
          assuranceDetailsRequired: false, //可选
          billingAddressRequired: true, //可选
          billingAddressParametersFormat: "FULL", //可选 MIN
          phoneNumberRequired: false, //可选
          tokenizationSpecificationType: "PAYMENT_GATEWAY", //必填 PAYMENT_GATEWAY、DIRECT
          gateway: "example", //PAYMENT_GATEWAY时必填
          gatewayMerchantId: "exampleGatewayMerchantId", //PAYMENT_GATEWAY时必填
          protocolVersion: "", //DIRECT时必填
          publicKey: "", //DIRECT时必填
          // buildTokenizationSpecification: {//可选，此字段是为了方便开发者自定义构造tokenizationSpecification参数,设置此字段时，会覆盖掉`tokenizationSpecificationType`、`gateway`、`gatewayMerchantId`、`protocolVersion`、`publicKey`字段。(HBuilderX 3.5.1+支持)
          //   "type": "PAYMENT_GATEWAY",
          //   "parameters": {
          //     "gateway": "custom-gateway",
          //     "gatewayMerchantId": "mock-gatewayMerchantId"
          //   }
          // }
        };


        let paypalPaymentMethodConfig = {
          environment: 3, // 必填 1 是product  3是test
          paymentMethodType: paymentMethodType, //必填 CARD、PAYPAL
          existingPaymentMethodRequired: false, //可选 如果设置为true同时已经准备好了支付allowedPaymentMethods中的付款方式，isReadyToPay就会返回true。

          currencyCode: "USD", //必填
          countryCode: "US", //在欧洲经济区必填
          transactionId: "", //当你想要接收googlepay回调的时候必填
          totalPriceStatus: "FINAL", //必填  NOT_CURRENTLY_KNOWN、ESTIMATED、FINAL
          totalPrice: "111.00", //必填 满足正则格式^[0-9]+(\.[0-9][0-9])?$
          totalPriceLabel: "100heelo", //可选
          checkoutOption: "DEFAULT", //可选 DEFAULT、COMPLETE_IMMEDIATE_PURCHASE

          merchantName: "Example Merchant", //可选
          emailRequired: true, //可选
          shippingAddressRequired: true, //可选
          shippingPhoneNumberRequired: false, //可选
          allowedCountryCodes: ["US", "GB"], //可选
          merchantId: "MVHSBANAS6KSE", //必填
        };

        let statement;

        if (paymentMethodType === "CARD") {
          statement = {
            ...cardPaymentMethodConfig
          };
        } else {
          statement = {
            ...paypalPaymentMethodConfig
          };
        }

        console.log(JSON.stringify(statement));

        plus.payment.request(provider, statement, (result) => {
          console.log("支付成功 :" + JSON.stringify(result));
        }, (e) => {
          console.log("支付失败： " + JSON.stringify(e));
        })
      });

    },

  }
}
</script>

<style scoped>
.box {
  padding: 30rpx;
  box-sizing: border-box;
}
</style>
